package cn.xt.controller.houselist;

/**
 * @author ifdestiny
 * @create 2018-11-22 10:53
 */

import cn.xt.bean.*;
import cn.xt.service.com.ComService;
import cn.xt.service.houselist.CityaService;
import cn.xt.service.houselist.HouseListService;
import cn.xt.util.Message;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Controller
@RequestMapping("/houses")
public class HouseListController {

    @Autowired
    private  HouseListService houseListService;
    @Autowired
    private CityaService cityaService;
    @Autowired
    private ComService ComService;

    /**
     * 查询房子信息
     * @param houseCondition 房子信息 条件
     * @param pageNo  页码
     * @return
     */
    @RequestMapping(value = "houseInfo")
    @ResponseBody
    public Message houselist(HouseCondition houseCondition,
                             @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo){
        //引入PageHelper分页插件在查询之前调用，传入页码 以及页面大小
        PageHelper.startPage(pageNo, 5);
        //startPage后面紧跟的就是一个分页查询
        List<HouseInfo> houseList = houseListService.getHouseList(houseCondition);
        PageInfo pageInfo = new PageInfo(houseList);
        return Message.success().add("pageInfo",pageInfo);
    }

    /**
     * 跳转房子查询页面
     * @param houseCondition
     * @param pageNo
     * @param model
     * @return
     */
    @RequestMapping(value = "housePage",produces = "text/html;charset=UTF-8")
    public String houseInfoPage(HouseCondition houseCondition, @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,Model model) throws UnsupportedEncodingException {
        System.out.println("houseCondition = " + houseCondition);
        PageHelper.startPage(pageNo, 5);
        List<HouseInfo> houseList = houseListService.getHouseList(houseCondition);
        PageInfo pageInfo = new PageInfo(houseList);
        model.addAttribute("houseCondition",houseCondition)
                .addAttribute("pageInfo",pageInfo);
        return "entire";
    }

    /**
     * 详情页面查询
     * @param houseid
     * @param model
     * @param session
     * @return
     */
    @RequestMapping(value = "houseDetails/{houseId}",produces = "text/html;charset=UTF-8")
    public String houseDetails(@PathVariable("houseId")Integer houseid, Model model, HttpSession session){
        HouseInfo houseInfo = houseListService.getHouseDetail(houseid);
        model.addAttribute("houseInfo",houseInfo);
        //收藏
        Users users=(Users) session.getAttribute("user");
       if(users==null){
           return "house-details";
       }else{
           Collectionhouse collection= ComService.collection(users.getUsers_id(),houseid);
           model.addAttribute("collection",collection);
           int count=ComService.countusers(users.getUsers_id(),houseid);
           model.addAttribute("count", count);
           int countadd=ComService.countadd(users.getUsers_id(),houseid);
           if (countadd==0){
               Collectionhouse collectionhouse=new Collectionhouse();
               Users users1=new Users();
               users1.setUsers_id(users.getUsers_id());
               House house=new House();
               house.setHouse_id(houseid);
               collectionhouse.setUsers(users1);
               collectionhouse.setHouse(house);
               ComService.addcollocation(collectionhouse);
           }

       }
        return "house-details";
    }

    /**
     * 房子 热门推荐数据
     * @return
     */
    @RequestMapping(value = "houseRecom")
    @ResponseBody
    public  Message houseRecommend(){
        List<HouseInfo> houseList = houseListService.getHouseRecommend();
        return  Message.success().add("houseList",houseList);
    }


    /**
     * 返回数据库城市 json 数据
     * @return
     */
    @RequestMapping(value = "json")
    @ResponseBody
    public List<Citya> json(){
        return cityaService.getCitya();
    }
}
